package org.jeecg.modules.demo.base.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.demo.base.entity.*;
import org.jeecg.modules.demo.base.service.IZnzfApprovalAppartAdminService;
import org.jeecg.modules.demo.base.service.IZnzfEmployeeScoreItemService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.base.service.IZnzfScoreStandardItemService;
import org.jeecg.modules.demo.base.service.IZnzfScoreStandardService;
import org.jeecg.modules.demo.definedtool.Paging;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 职工计分包含的标准项
 * @Author: jeecg-boot
 * @Date:   2022-03-29
 * @Version: V1.0
 */
@Api(tags="职工计分包含的标准项")
@RestController
@RequestMapping("/base/znzfEmployeeScoreItem")
@Slf4j
public class ZnzfEmployeeScoreItemController extends JeecgController<ZnzfEmployeeScoreItem, IZnzfEmployeeScoreItemService> {
	@Autowired
	private IZnzfEmployeeScoreItemService znzfEmployeeScoreItemService;

	@Autowired
	private IZnzfApprovalAppartAdminService znzfApprovalAppartAdminService;

	@Autowired
	private ISysUserService sysUserService;

	 @Autowired
	 private IZnzfScoreStandardItemService znzfScoreStandardItemService;

	 @Autowired
	 private IZnzfScoreStandardService znzfScoreStandardService;

	/**
	 * 分页列表查询
	 *
	 * @param znzfEmployeeScoreItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "职工计分包含的标准项-分页列表查询")
	@ApiOperation(value="职工计分包含的标准项-分页列表查询", notes="职工计分包含的标准项-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZnzfEmployeeScoreItem>> queryPageList(ZnzfEmployeeScoreItem znzfEmployeeScoreItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZnzfEmployeeScoreItem> queryWrapper = QueryGenerator.initQueryWrapper(znzfEmployeeScoreItem, req.getParameterMap());
		Page<ZnzfEmployeeScoreItem> page = new Page<ZnzfEmployeeScoreItem>(pageNo, pageSize);
		IPage<ZnzfEmployeeScoreItem> pageList = znzfEmployeeScoreItemService.page(page, queryWrapper);
		List<ZnzfEmployeeScoreItem> znzfEmployeeScoreItemList = pageList.getRecords();
		for(ZnzfEmployeeScoreItem znzfEmployeeScoreItem1 : znzfEmployeeScoreItemList){
			SysUser sysUser = sysUserService.getById(znzfEmployeeScoreItem1.getUserId());
			znzfEmployeeScoreItem1.setUserName(sysUser.getRealname());
			znzfEmployeeScoreItem1.setUserTel(sysUser.getPhone());
			ZnzfScoreStandardItem znzfScoreStandardItem = znzfScoreStandardItemService.getById(znzfEmployeeScoreItem1.getScoreItemId());
			znzfEmployeeScoreItem1.setSSIName(znzfScoreStandardItem.getName());
			znzfEmployeeScoreItem1.setSSIScore(znzfScoreStandardItem.getScore());
			znzfEmployeeScoreItem1.setSSINote(znzfScoreStandardItem.getNote());
			ZnzfScoreStandard znzfScoreStandard = znzfScoreStandardService.getById(znzfScoreStandardItem.getScoreStandardId());
			znzfEmployeeScoreItem1.setSSTitle(znzfScoreStandard.getTitle());
			znzfEmployeeScoreItem1.setSSNo(znzfScoreStandard.getNo());
		}
		pageList.setRecords(znzfEmployeeScoreItemList);
		return Result.OK(pageList);
	}

	@ApiOperation(value="职工计分包含的标准项-新分页列表查询", notes="职工计分包含的标准项-新分页列表查询")
	@GetMapping(value = "/voList")
	public Result<Page<ZnzfEmployeeScoreItem>> queryVoPageList(HttpServletRequest request,
															   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		String tmp = JwtUtil.getUsername(token);
		SysUser sysUser = sysUserService.getUserByName(tmp);
		ZnzfApprovalAppartAdmin admin = znzfApprovalAppartAdminService.getAdminByUserId(sysUser.getId());
		// 无权限
		if (admin == null) {
			return Result.error("暂无该项资质查询权限", null);
		}
		List<ZnzfEmployeeScoreItem> list = znzfEmployeeScoreItemService.queryAllEmployeeScoreItemList();
		Paging<ZnzfEmployeeScoreItem> paging = new Paging<>();
		Result<Page<ZnzfEmployeeScoreItem>> result = paging.getPaging(list, pageNo, pageSize);
		return result;
	}
	
	/**
	 *   添加
	 *
	 * @param znzfEmployeeScoreItem
	 * @return
	 */
	@AutoLog(value = "职工计分包含的标准项-添加")
	@ApiOperation(value="职工计分包含的标准项-添加", notes="职工计分包含的标准项-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZnzfEmployeeScoreItem znzfEmployeeScoreItem) {
		znzfEmployeeScoreItemService.save(znzfEmployeeScoreItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param znzfEmployeeScoreItem
	 * @return
	 */
	@AutoLog(value = "职工计分包含的标准项-编辑")
	@ApiOperation(value="职工计分包含的标准项-编辑", notes="职工计分包含的标准项-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZnzfEmployeeScoreItem znzfEmployeeScoreItem) {
		znzfEmployeeScoreItemService.updateById(znzfEmployeeScoreItem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "职工计分包含的标准项-通过id删除")
	@ApiOperation(value="职工计分包含的标准项-通过id删除", notes="职工计分包含的标准项-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		znzfEmployeeScoreItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "职工计分包含的标准项-批量删除")
	@ApiOperation(value="职工计分包含的标准项-批量删除", notes="职工计分包含的标准项-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.znzfEmployeeScoreItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "职工计分包含的标准项-通过id查询")
	@ApiOperation(value="职工计分包含的标准项-通过id查询", notes="职工计分包含的标准项-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZnzfEmployeeScoreItem> queryById(@RequestParam(name="id",required=true) String id) {
		ZnzfEmployeeScoreItem znzfEmployeeScoreItem = znzfEmployeeScoreItemService.getById(id);
		if(znzfEmployeeScoreItem==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfEmployeeScoreItem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param znzfEmployeeScoreItem
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZnzfEmployeeScoreItem znzfEmployeeScoreItem) {
        return super.exportXls(request, znzfEmployeeScoreItem, ZnzfEmployeeScoreItem.class, "职工计分包含的标准项");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZnzfEmployeeScoreItem.class);
    }



	 /**
	  *   提交审核：批量插入一个职工的计分标准项
	  *
	  * @param znzfEmployeeScoreItemParam  前端的提交审核的对象
	  * @return
	  */
	 @AutoLog(value = "职工计分的所有计分标准项-添加")
	 @ApiOperation(value="职工计分的所有计分标准项-添加", notes="职工计分的所有计分标准项-添加")
	 @PostMapping(value = "/insertEmployeeScoreItemList")
     public Result insertEmployeeScoreItemList(@RequestBody ZnzfEmployeeScoreItemListParam znzfEmployeeScoreItemParam) {


	 	int res = znzfEmployeeScoreItemService.insertAllSocreItem2(znzfEmployeeScoreItemParam);
	 	if (res == 0) return Result.error("无法重复插入数据",null);
	 	return Result.OK("添加成功！");
	 }



	 /**
	  *   显示职员的历史资质
	  *
	  * @param userId
	  * @return
	  */
	 @AutoLog(value = "职工计分的所有计分标准项-显示职员的历史资质")
	 @ApiOperation(value="职工计分的所有计分标准项-显示职员的历史资质", notes="职工计分的所有计分标准项-显示职员的历史资质")
	 @PostMapping(value = "/getEmployeeScoreItemList")
	 public Result<List<ZnzfEmployeeScoreItem>> queryEmployeeScoreItemList(@RequestParam(name="userId",required=true) String userId) {
		 List<ZnzfEmployeeScoreItem> itemList = znzfEmployeeScoreItemService.queryEmployeeScoreItemListByUid(userId);
		 return Result.OK(itemList);
	 }


	 /**
	  * 根据userId添加总分
	  * @param userId
	  * @param latestScore
	  * @return
	  */
	 @AutoLog(value = "职工计分的所有计分标准项-添加总分")
	 @ApiOperation(value="职工计分的所有计分标准项-添加总分", notes="职工计分的所有计分标准项-添加总分")
	 @PostMapping(value = "/insertLatestScoreByUserId")
	 public Result insertLatestScoreByUserId(@RequestParam(name="userId",required=true) String userId,
											   @RequestParam(name="latestScore",required=true) String latestScore) {

	 	 int res = znzfEmployeeScoreItemService.insertLatestScoreByUserId(userId, latestScore);
	 	 if (res == 0) {
	 	 	return Result.error("更新总分失败",null);
		 }
		 return Result.OK("更新总分成功！");
	 }

	 /**
	  * 根据userId查询总分
	  * @param userId
	  * @return
	  */
	 @AutoLog(value = "职工计分的所有计分标准项-查询总分")
	 @ApiOperation(value="职工计分的所有计分标准项-查询总分", notes="职工计分的所有计分标准项-查询总分")
	 @PostMapping(value = "/queryLatestScoreByUserId")
	 public Result queryLatestScoreByUserId(@RequestParam(name="userId",required=true) String userId) {

		 Double latestScore = znzfEmployeeScoreItemService.queryLatestScoreByUserId(userId);
		 if (latestScore != null) {
			 return Result.OK("查询总分成功",latestScore);
		 } else {
		 	return Result.error("暂无总分",null);
		 }

	 }

}
